//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "DriftTermStructure.h"
using namespace Cephei::QL::Termstructures::Yield;
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Termstructures/Volatility/Equityfx/BlackVolTermStructure.h>
#include <gen/QL/Quote.h>
#include <gen/QL/InterestRate.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Termstructures/Yield/ZeroYieldStructure.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL::Termstructures::Volatility::Equityfx;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Yield::CDriftTermStructure::CDriftTermStructure (Cephei::QL::Termstructures::IYieldTermStructure^ riskFreeTS, Cephei::QL::Termstructures::IYieldTermStructure^ dividendTS, Cephei::QL::Termstructures::Volatility::Equityfx::IBlackVolTermStructure^ blackVolTS) : CZeroYieldStructure(CDriftTermStructure::typeid)
{
    CYieldTermStructure^ _CriskFreeTS;
    CYieldTermStructure^ _CdividendTS;
    CBlackVolTermStructure^ _CblackVolTS;
    try
    {
#ifdef HANDLE
        _phDriftTermStructure = NULL;
#endif
        _CriskFreeTS = safe_cast<CYieldTermStructure^> (riskFreeTS);
        _CriskFreeTS->Lock();
        Handle<QuantLib::YieldTermStructure>& _riskFreeTS = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CriskFreeTS->GetHandle ()); 
        _CdividendTS = safe_cast<CYieldTermStructure^> (dividendTS);
        _CdividendTS->Lock();
        Handle<QuantLib::YieldTermStructure>& _dividendTS = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CdividendTS->GetHandle ()); 
        _CblackVolTS = safe_cast<CBlackVolTermStructure^> (blackVolTS);
        _CblackVolTS->Lock();
        Handle<QuantLib::BlackVolTermStructure>& _blackVolTS = static_cast<Handle<QuantLib::BlackVolTermStructure>&> (_CblackVolTS->GetHandle ()); 
        _ppDriftTermStructure = new boost::shared_ptr<QuantLib::DriftTermStructure> (new QuantLib::DriftTermStructure ( _riskFreeTS,  _dividendTS,  _blackVolTS ));
        SetZeroYieldStructure (boost::dynamic_pointer_cast<QuantLib::ZeroYieldStructure> (*_ppDriftTermStructure));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CriskFreeTS != nullptr) _CriskFreeTS->Unlock();
        if (_CdividendTS != nullptr) _CdividendTS->Unlock();
        if (_CblackVolTS != nullptr) _CblackVolTS->Unlock();
    }
}
Cephei::QL::Termstructures::Yield::CDriftTermStructure::CDriftTermStructure (boost::shared_ptr<QuantLib::DriftTermStructure>& childNative, Object^ owner) : CZeroYieldStructure(CDriftTermStructure::typeid)
{
#ifdef HANDLE
	_phDriftTermStructure = NULL;
#endif
	_ppDriftTermStructure = &childNative;
    _ppZeroYieldStructure = new boost::shared_ptr<QuantLib::ZeroYieldStructure> (boost::dynamic_pointer_cast<QuantLib::ZeroYieldStructure> (*_ppDriftTermStructure));
}
Cephei::QL::Termstructures::Yield::CDriftTermStructure::CDriftTermStructure (QuantLib::DriftTermStructure& childNative, Object^ owner) : CZeroYieldStructure(CDriftTermStructure::typeid)
{
#ifdef HANDLE
	_phDriftTermStructure = NULL;
#endif
	_ppDriftTermStructure = new boost::shared_ptr<QuantLib::DriftTermStructure> (&childNative);
    _ppZeroYieldStructure = new boost::shared_ptr<QuantLib::ZeroYieldStructure> (boost::dynamic_pointer_cast<QuantLib::ZeroYieldStructure> (*_ppDriftTermStructure));
    _DriftTermStructureOwner = owner;
    _ZeroYieldStructureOwner = owner;
}

Cephei::QL::Termstructures::Yield::CDriftTermStructure::CDriftTermStructure (CDriftTermStructure^ copy) : CZeroYieldStructure(CDriftTermStructure::typeid)
{
#ifdef HANDLE
	_phDriftTermStructure = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppDriftTermStructure = new boost::shared_ptr<QuantLib::DriftTermStructure> (copy->GetShared());
        _ppZeroYieldStructure = new boost::shared_ptr<QuantLib::ZeroYieldStructure> (boost::dynamic_pointer_cast<QuantLib::ZeroYieldStructure> (*_ppDriftTermStructure));
    }
}
Cephei::QL::Termstructures::Yield::CDriftTermStructure::CDriftTermStructure (System::Type^ t) : CZeroYieldStructure(CDriftTermStructure::typeid)
{
#ifdef HANDLE
	_phDriftTermStructure = NULL;
#endif
	if (!t->IsSubclassOf(CDriftTermStructure::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Yield::CDriftTermStructure::CDriftTermStructure (QuantLib::Handle<QuantLib::DriftTermStructure>& childNative, Object^ owner)  : CZeroYieldStructure(CDriftTermStructure::typeid)
{
	_phDriftTermStructure = &childNative;
	_ppDriftTermStructure = &static_cast<boost::shared_ptr<QuantLib::DriftTermStructure>>(childNative.currentLink());
    _ppZeroYieldStructure = new boost::shared_ptr<QuantLib::ZeroYieldStructure> (boost::dynamic_pointer_cast<QuantLib::ZeroYieldStructure> (*_ppDriftTermStructure));
    _DriftTermStructureOwner = owner;
}
Cephei::QL::Termstructures::Yield::CDriftTermStructure::CDriftTermStructure (QuantLib::Handle<QuantLib::DriftTermStructure> childNative)  : CZeroYieldStructure(CDriftTermStructure::typeid)
{
	_phDriftTermStructure = &childNative;
	_ppDriftTermStructure = &static_cast<boost::shared_ptr<QuantLib::DriftTermStructure>>(childNative.currentLink());
    _ppZeroYieldStructure = new boost::shared_ptr<QuantLib::ZeroYieldStructure> (boost::dynamic_pointer_cast<QuantLib::ZeroYieldStructure> (*_ppDriftTermStructure));
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Yield::CDriftTermStructure::CDriftTermStructure (QuantLib::DriftTermStructure childNative)  : CZeroYieldStructure(CDriftTermStructure::typeid)
{
#ifdef HANDLE
	_phDriftTermStructure = NULL;
#endif
	_ppDriftTermStructure = new boost::shared_ptr<QuantLib::DriftTermStructure> (new QuantLib::DriftTermStructure (childNative));
    _ppZeroYieldStructure = new boost::shared_ptr<QuantLib::ZeroYieldStructure> (boost::dynamic_pointer_cast<QuantLib::ZeroYieldStructure> (*_ppDriftTermStructure));
}
#endif

Cephei::QL::Termstructures::Yield::CDriftTermStructure::~CDriftTermStructure ()
{
    if (_ppDriftTermStructure != NULL)
    {
	    delete _ppDriftTermStructure;
        _ppDriftTermStructure = NULL;
    }
}
Cephei::QL::Termstructures::Yield::CDriftTermStructure::!CDriftTermStructure ()
{
    if (_ppDriftTermStructure != NULL)
    {
	    delete _ppDriftTermStructure;
    }
}
QuantLib::DriftTermStructure& Cephei::QL::Termstructures::Yield::CDriftTermStructure::GetReference ()
{
    if (_ppDriftTermStructure == NULL) throw gcnew NativeNullException ();
	return **_ppDriftTermStructure;
}
boost::shared_ptr<QuantLib::DriftTermStructure>& Cephei::QL::Termstructures::Yield::CDriftTermStructure::GetShared ()
{
    if (_ppDriftTermStructure == NULL) throw gcnew NativeNullException ();
	return *_ppDriftTermStructure;
}
QuantLib::DriftTermStructure* Cephei::QL::Termstructures::Yield::CDriftTermStructure::GetPointer ()
{
    if (_ppDriftTermStructure == NULL) throw gcnew NativeNullException ();
	return &**_ppDriftTermStructure;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::DriftTermStructure>& Cephei::QL::Termstructures::Yield::CDriftTermStructure::GetHandle ()
{
	if (_phDriftTermStructure == NULL)
	{
		_phDriftTermStructure = new Handle<QuantLib::DriftTermStructure> (*_ppDriftTermStructure);
	}
	return *_phDriftTermStructure;
}
#endif
bool Cephei::QL::Termstructures::Yield::CDriftTermStructure::HasNative () 
{
	return (_ppDriftTermStructure != NULL);
}

Cephei::QL::Times::ICalendar^ Cephei::QL::Termstructures::Yield::CDriftTermStructure::Calendar::get ()
{
    try
    {
    	QuantLib::Calendar _rv = (QuantLib::Calendar)(*_ppDriftTermStructure)->calendar ( );   
        Cephei::QL::Times::CCalendar^ _nrv = gcnew Cephei::QL::Times::CCalendar (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::IDayCounter^ Cephei::QL::Termstructures::Yield::CDriftTermStructure::DayCounter::get ()
{
    try
    {
    	QuantLib::DayCounter _rv = (QuantLib::DayCounter)(*_ppDriftTermStructure)->dayCounter ( );   
        Cephei::QL::Times::CDayCounter^ _nrv = gcnew Cephei::QL::Times::CDayCounter (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Termstructures::Yield::CDriftTermStructure::MaxDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppDriftTermStructure)->maxDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Termstructures::Yield::CDriftTermStructure::ReferenceDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppDriftTermStructure)->referenceDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt32 Cephei::QL::Termstructures::Yield::CDriftTermStructure::SettlementDays::get ()
{
    try
    {
    	QuantLib::Natural _rv = (QuantLib::Natural)(*_ppDriftTermStructure)->settlementDays ( );   
        UInt32 _nrv = (UInt32)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Termstructures::Yield::IDriftTermStructure^ Cephei::QL::Termstructures::Yield::CDriftTermStructure_Factory::Create (Cephei::QL::Termstructures::IYieldTermStructure^ riskFreeTS, Cephei::QL::Termstructures::IYieldTermStructure^ dividendTS, Cephei::QL::Termstructures::Volatility::Equityfx::IBlackVolTermStructure^ blackVolTS)
{
    return gcnew CDriftTermStructure ( riskFreeTS,  dividendTS,  blackVolTS);
}
